$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'etcc/investment/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '报单名', name: 'name', index: 'name', width: 80 }, 			
			{ label: '收益', name: 'profit', index: 'profit', width: 80 }, 			
			{ label: '收益提现', name: 'profitCash', index: 'profit_cash', width: 80 },
			{ label: '收益商城积分', name: 'profitIntegral', index: 'profit_integral', width: 80 },
			{ label: '收益ETCC（锁仓）', name: 'profitEtcc', index: 'profit_etcc', width: 80 },
			{ label: '收益锁仓天数', name: 'profitDay', index: 'profit_day', width: 80 },
			{ label: '按收益/天释放', name: 'profitRate', index: 'profit_rate', width: 80 },
			{ label: '赎回商城积分', name: 'redeemIntegral', index: 'redeem_integral', width: 80 },
			{ label: '赎回ETCC', name: 'redeemEtcc', index: 'redeem_etcc', width: 80 },
			{ label: '赎回锁仓天数', name: 'redeemDay', index: 'redeem_day', width: 80 },
			{ label: '按赎回/天释放', name: 'redeemRate', index: 'redeem_rate', width: 80 },
			{ label: '总动态奖励变现', name: 'dynamicCash', index: 'dynamic_cash', width: 80 },
			{ label: '总动态奖励转ETCC', name: 'dynamicEtcc', index: 'dynamic_etcc', width: 80 },
			{ label: '总动态奖励锁仓天数', name: 'dynamicDay', index: 'dynamic_day', width: 80 },
			{ label: '按总动态奖励/天释放', name: 'dynamicRate', index: 'dynamic_rate', width: 80 },
			{ label: '开始时间', name: 'startTime', index: 'start_time', width: 80 }, 			
			{ label: '结束时间', name: 'endTime', index: 'end_time', width: 80 }, 			
			{ label: '人数', name: 'number', index: 'number', width: 80 }, 			
			{ label: '总额度', name: 'amount', index: 'amount', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		investment: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.investment = {profit:30,profitCash:15,profitIntegral:6,profitEtcc:9,profitDay:30,profitRate:1,redeemIntegral:2,redeemEtcc:1.5,
				redeemDay:90,redeemRate:0.5,dynamicCash:70,dynamicEtcc:30,dynamicDay:30,dynamicRate:1
			};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.investment.id == null ? "etcc/investment/save" : "etcc/investment/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.investment),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "etcc/investment/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "etcc/investment/info/"+id, function(r){
                vm.investment = r.investment;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});